package com.caocaog.market.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caocaog.market.entity.Member;
import com.caocaog.market.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.Trim;
import org.jeecg.common.system.base.controller.GenericController;
import org.jeecg.common.system.domain.QueryRequest;
import org.jeecg.common.system.query.PageGenerator;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 会员信息
 *
 * @author chechen
 * @date 2021-11-19
 */
@Slf4j
@RestController
@Api(value = "member", tags = MemberController.TABLE_MEANING)
@RequestMapping("/member")
public class MemberController extends GenericController<Member, MemberService> {

	public static final String TABLE_MEANING = "会员信息";

	private static final String PAGE = "会员信息-分页列表查询";
	@Trim
	@ApiOperation(value = PAGE, notes = PAGE)
	@GetMapping(value = "/list")
	@RequiresPermissions("member:view")
	public Result<?> queryMemberPage(Member member, QueryRequest queryRequest) {
		Page<Member> page = PageGenerator.createPageSort(queryRequest);
		IPage<Member> memberIPage = super.service.queryMemberPage(page, member);
		return Result.OK(memberIPage);
	}

	private static final String ADD = "会员信息-添加";
	@AutoLog(value = ADD)
	@ApiOperation(value = ADD, notes = ADD)
	@PostMapping(value = "/add")
	@RequiresPermissions("member:add")
	public Result<?> add(@RequestBody Member member) {
		return Result.OK(ADD_SUCCESS, super.service.addMember(member));
	}

	private static final String EDIT = "会员信息-编辑";
	@AutoLog(value = EDIT)
	@ApiOperation(value = EDIT, notes = EDIT)
	@PutMapping(value = "/edit")
	@RequiresPermissions("member:edit")
	public Result<?> edit(@RequestBody Member member) {
		return Result.OK(EDIT_SUCCESS, super.service.updateMember(member));
	}

	private static final String DELETE = "会员信息-通过id删除";
	@AutoLog(value = DELETE)
	@ApiOperation(value = DELETE, notes = DELETE)
	@DeleteMapping(value = "/delete")
	@RequiresPermissions("member:delete")
	public Result<?> delete(@RequestParam(name = "id") String id) {
		return Result.OK(DELETE_SUCCESS, super.service.deleteMember(id));
	}

	private static final String DELETE_BATCH = "会员信息-批量删除";
	@AutoLog(value = DELETE_BATCH)
	@ApiOperation(value = DELETE_BATCH, notes = DELETE_BATCH)
	@DeleteMapping(value = "/deleteBatch")
	@RequiresPermissions("member:delete")
	public Result<?> deleteBatch(@NotNull @RequestParam(name = "ids") String ids) {
		return Result.OK(DELETE_SUCCESS, super.service.deleteBatchMember(Arrays.asList(ids.split(","))));
	}

	private static final String BY_ID = "会员信息-通过id查询";
	@AutoLog(value = BY_ID)
	@ApiOperation(value = BY_ID, notes = BY_ID)
	@GetMapping(value = "/queryById")
	@RequiresPermissions("member:view")
	public Result<?> queryById(@RequestParam(name = "id") String id) {
		return Result.OK(super.service.queryMemberOneById(id));
	}

	private static final String STATUS_CHANGE = "会员信息-状态变更";
	@AutoLog(value = STATUS_CHANGE)
	@ApiOperation(value = STATUS_CHANGE, notes = STATUS_CHANGE)
	@PutMapping(value = "/statusChange")
	@RequiresPermissions("member:status")
	public Result<?> statusChange(@RequestParam(name = "id") String id) {
		return Result.OK("状态变更成功", super.service.memberStatusChange(id));
	}

}
